package com.lyl.module.listener;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.core.util.ObjectUtil;
import com.lyl.module.api.service.LoginApiService;
import com.lyl.module.constant.ALIYUN_GLOBAL_VARIABLE;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * @author 97134
 */
@Slf4j
@Component
@Order(0)
@RequiredArgsConstructor
public class MyApplicationListener implements ApplicationListener<ApplicationReadyEvent> {
    private final LoginApiService loginApiService;

    @Override
    public void onApplicationEvent(ApplicationReadyEvent applicationReadyEvent) {
        log.info("初始化阿里云盘配置");
        if (CharSequenceUtil.isBlank(ALIYUN_GLOBAL_VARIABLE.REFRESH_TOKEN)){
            log.error("当前账号未登录");
            throw new RuntimeException("当前账号未登录");
        }
        if (ObjectUtil.isNull(ALIYUN_GLOBAL_VARIABLE.NEXT_REFRESH_TOKEN_TIME) || DateUtil.date().isAfter(ALIYUN_GLOBAL_VARIABLE.NEXT_REFRESH_TOKEN_TIME)){
            log.info("刷新token");
            loginApiService.refreshToken();
        }
    }
}
